/* eslint-disable new-cap */
/*
 * @Author: yangrongxin
 * @Date: 2021-04-13 15:50:12
 * @LastEditors: guoju
 * @LastEditTime: 2022-01-10 14:07:04
 */
import 'reflect-metadata';

import { REQUEST_TYPE, CONTEXT_PATH } from '../const';

import { RequestType } from '../enum';

import { toString } from '../utils';

class RequestDecorator {
  static invokeRequestType(requestType: RequestType, target: any, name: any, descriptor: any) {
    // console.log('targetKeys', Object.keys(target));
    // console.log('targetValues', Object.values(target));
    // console.log('targetName', target.name);
    // console.log('name', new target().name);
    Reflect.defineMetadata(REQUEST_TYPE, requestType, target, target[name]);
    if (descriptor?.initializer) descriptor.initializer = () => toString(new target().name, target[name]);
    else target[name] = toString(new target().name, target[name]);
  }
  static Get(): Function {
    return function (target: any, name: any, descriptor: any) {
      RequestDecorator.invokeRequestType(RequestType.GET, target, name, descriptor);
    };
  }

  static Post(): Function {
    return function (target: any, name: any, descriptor: any) {
      RequestDecorator.invokeRequestType(RequestType.POST, target, name, descriptor);
    };
  }

  static Delete(): Function {
    return function (target: any, name: any, descriptor: any) {
      RequestDecorator.invokeRequestType(RequestType.DELETE, target, name, descriptor);
    };
  }
  static Patch(): Function {
    return function (target: any, name: any, descriptor: any) {
      RequestDecorator.invokeRequestType(RequestType.PATCH, target, name, descriptor);
    };
  }
}

function Controller(contextPath: string) {
  return function (target: any) {
    // console.log('target', target);
    // console.log('arguments', arguments);
    // console.log(Reflect.ownKeys(target));
    Reflect.defineMetadata(CONTEXT_PATH, contextPath, target);
  };
}

export { Controller };

export default RequestDecorator;
